package com.etl.controller;

import com.etl.entity.EtlBusinessDbInfo;
import com.etl.enums.ResultEnum;
import com.etl.model.rev.DbInfoSaveUpdateRev;
import com.etl.model.rev.TableStructureRev;
import com.etl.service.IEtlBusinessDBInfoService;
import com.etl.util.ResponseUtil;
import com.etl.util.ResultVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2019/10/21.
 */
@RestController
@RequestMapping("/etlBusinessDbInfo")
@Api(tags = {"数据库管理"}, value = "数据库信息增删改查相关接口")
@Validated
public class EtlBusinessDbInfoController {

    @Autowired
    private IEtlBusinessDBInfoService etlBusinessDBInfoService;


    @ApiOperation(value = "测试数据库连接")
    @PutMapping(value = "/testConnection")
    public ResultVO testConnection(@RequestBody DbInfoSaveUpdateRev dbInfoSaveUpdateRev) {
        try {
            etlBusinessDBInfoService.testDBConnection(dbInfoSaveUpdateRev);

        } catch (SQLException e) {
            return ResponseUtil.error(e.getErrorCode(), e.getMessage());
        }
        return ResponseUtil.success();
    }



    @ApiOperation(value = "查询已添加的数据库列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pages", value = "页数", example = "1"),
            @ApiImplicitParam(paramType = "query", name = "sizes", value = "数量", example = "10")
    })
    @GetMapping(value = "/getEtlBusinessDbInfoList")
    public ResultVO<PageInfo<EtlBusinessDbInfo>> getEtlDBList(Integer pages, Integer sizes) {
        return ResponseUtil.success(etlBusinessDBInfoService.getEtlBusinessDbInfoList(pages, sizes));
    }



    @ApiOperation(value = "添加数据库")
    @PutMapping(value = "/saveUpdateEtlBusinessDbInfo")
    public ResultVO saveUpdateEtlBusinessDbInfo(@RequestBody DbInfoSaveUpdateRev dbInfoSaveUpdateRev) {
        etlBusinessDBInfoService.saveUpdateEtlBusinessDbInfo(dbInfoSaveUpdateRev);
        return ResponseUtil.success();
    }

    @ApiOperation(value = "删除数据库")
    @ApiImplicitParam(paramType = "query", name = "dbId", value = "主键id", example = "10")
    @GetMapping(value = "/deleteEtlBusinessDbInfo")
    public ResultVO deleteEtlBusinessDbInfo(@NotNull(message = "数据库id不能为空") Integer dbId) {
        etlBusinessDBInfoService.deleteEtlBusinessDbInfo(dbId);
        return ResponseUtil.success();
    }


    @ApiOperation(value = "查询已添加数据库的业务系统")
    @GetMapping(value = "/querySystemName")
    public ResultVO<List<String>> querySystemName() {
        List<String> names = etlBusinessDBInfoService.querySystemName();
        return ResponseUtil.success(names);
    }


}

